<?php
class LDAP {
    var $DEBUGSTATE=FALSE,
        $link,
        $userid = null,
        $exist;
    
    /*
     * Function returns variuos information about objects.
     * @param1 - object to return information
     * @ptype1 - object
     * @param2 - optional text about object
     * @ptype2 - string
     * @return - information and note about object
     * @rtype - string
     */
    private function DEBUG($msg, $text="") {
        if ($this->DEBUGSTATE) {
            echo "<div style='height:100px; width:90%; background:yellow; color:black; border: 1px solid black;";
            echo "padding: 5px; margin: 5px; white-space: pre;overflow:scroll;overflow-x: hidden;'>";
            echo "<div style='background:lightblue;'>" . $text . "</div>";
            var_dump($msg);
            echo "</div>";
        }
    }//DEBUG().
    
    function __construct($host, $domain, $username=null, $password=null, $port="389") {
        require_once ("db.class.php");
        include("mysql.config.php");
        include ("user.class.php");
        
        $this->DEBUG("Connect", "CONNECT TO LDAP (construct())");
        $ldapconn = ldap_connect($host, $port) or die (" <p style='color:red;'> Could not connect to $host </p>");
        ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3); 
        $this->DEBUG($host, "Connected (construct())");
        
        if (isset($username)) {
            if ((string)$password == "")
                $password = "null";
            $res = ldap_bind($ldapconn, "cn=" . $username . "," . $domain, $password);
            $this->DEBUG($res, $username . "BIND (construct())");
       
            if ($res) {
                $this->DEBUG("LENDAM I MYSQL", $db_address);
                $this->link = new DataBase($db_address, $db_username, $db_password, $db_db);
                $this->DEBUG("MYSQL KONSTRUKTORIUS");
                $this->link->select("`users`", "*", "`name`='$username'");
                $res = $this->link->getCount();
                $this->DEBUG($res, "MYSQL GET COUNT (construct())");
                if ($res == 0) {
                    $usr = new User($username, "surname", "email");
                    $this->userid = $usr->getId();
                    $this->DEBUG("$this->userid", "USER ID NEW (construct())");
                } else {
                    $this->link->select("`users`", "*", "`name`='$username'");
                    $t = $this->link->getResult();
                    $this->userid = $t['id'];
                    $this->DEBUG("$this->userid", "USER ID exists (construct())");
                }
            } else {
                //nera tokio vartotojo
            }
           
            //check mysql jei neegzistuoja imest, jei egzistuoja GRAZINT TRUE;
        } else {
            
        }
        
     
    }//__construct
    
    function getResult() {
        if (isset($this->userid)) 
            return $this->userid;
        else
            return null;
    }//getResult
      
}//LDAP class
?>
